package com.anysoft.web;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.webkit.WebView;

import com.anysoft.WebViewNetworkProtocol;
import com.anysoft.event.TimeOutEvent;
import com.anysoft.me.base.BaseWebViewActivity;
import com.anysoft.utils.ActivityUtils;
import com.anysoft.utils.CommonUtil;
import com.anysoft.utils.HttpUtils;
import com.anysoft.utils.MentorLogger;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 理财日历-历史累计
 * Created by wyb on 2017/4/7.
 */

public class CalendarHistoryActivity extends BaseWebViewActivity {

    private String token = "";

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isShowTitleBar(true);
        title.setText("历史累计");
        token = getToken();
        generateUrl("calendarHistory", HttpUtils.getCommonParams2StringByWeb(mActivity));
        if (CommonUtil.isNetWorkConnected(mActivity)) {
            setWebViewConfig(loadUrl);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!token.equals(getToken())) {
            token = getToken();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void onshouldOverrideUrlLoadingCallBack(WebView webview, String url) {
        MentorLogger.Log(TAG, "url=" + url);
//        if (url.contains("assetsFundDetail")) {
//            String param = url.split("\\?")[1];
//            System.out.println("param---" + param);
//            mIntent.putExtra("param", param);
//            mIntent.setClass(mActivity, AssetsFundDetailActivity.class);
//            startActivity(mIntent);
//        } else
        if (url.contains("prompt.html")) {
            mIntent.setClass(mActivity, SelectWarmPromptActivity.class);
            mIntent.putExtra("url", url);
            startActivity(mIntent);
        } else if (url.contains(WebViewNetworkProtocol.GOTO_LOGIN)) {
            ActivityUtils.gotoLogin(mActivity, hasUser(), false, findUserId());
            EventBus.getDefault().post(new TimeOutEvent(true));
        } else if (url.contains(WebViewNetworkProtocol.GO_QUESTION)) {
            startRiskActivity(null);
        } else {
            webview.loadUrl(url);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onTimeOutCallBack(TimeOutEvent event) {
        if (event.isSuccess) {
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
